package com.xq.jltx.frescosimple.fragment;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.xq.jltx.frescosimple.R;
import com.xq.jltx.frescosimple.utils.FrescoUtils;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016年02月04日.
 */

public class ImageMessageDialogFragment extends DialogFragment{
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private ImageView showImg;
    private TextView showTv;
    private Button cancleBtn;

    private Bitmap imgBmp;
    private Uri uri;

    private void assignViews(View view) {
        showImg = (ImageView) view.findViewById(R.id.show_img);
        showTv = (TextView) view.findViewById(R.id.show_tv);
        cancleBtn = (Button)view.findViewById(R.id.cancle);
    }


    public static ImageMessageDialogFragment newInstance(Uri uri){
        ImageMessageDialogFragment imageMessageDialogFragment = new ImageMessageDialogFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM1,uri);
        imageMessageDialogFragment.setArguments(args);
        return imageMessageDialogFragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        if(getArguments() != null){
            uri = getArguments().getParcelable(ARG_PARAM1);
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        Window dialogWindow = getDialog().getWindow();
        //代码去掉丑的标题栏
//        dialogWindow.requestFeature(Window.FEATURE_NO_TITLE);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.gravity = Gravity.BOTTOM;  //设置对话框的初始位置，默认不设置的时候是居中显示对话框的
        dialogWindow.setAttributes(lp);
        View view = inflater.inflate(R.layout.image_message_show,null,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        assignViews(view);
        if(!TextUtils.isEmpty(uri.getPath())){
            if(FrescoUtils.getTypeImageSequence(uri) == FrescoUtils.LOCAL_ASSET_SCHEME_INT){
                AssetManager assetManager = getContext().getAssets();
                try {
                    InputStream inputStream = assetManager.open(FrescoUtils.getLocalImageFile(uri));
                    imgBmp = BitmapFactory.decodeStream(inputStream);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else if(FrescoUtils.getTypeImageSequence(uri) == FrescoUtils.LOCAL_RESOURCE_SCHEME_INT){
                int resId = FrescoUtils.getResourceId(uri);
                imgBmp = BitmapFactory.decodeResource(getContext().getResources(),resId);
            }else if(FrescoUtils.getTypeImageSequence(uri) == FrescoUtils.LOCAL_FILE_SCHEME_INT){
                imgBmp = BitmapFactory.decodeFile(FrescoUtils.getLocalImageFile(uri));
            }

            if (imgBmp != null && !imgBmp.isRecycled()) {
                showImg.setImageBitmap(imgBmp);
            }
        }
        showTv.setText(FrescoUtils.getLocalImageFile(uri));
        cancleBtn.setOnClickListener(mOnClickListener);
        super.onViewCreated(view, savedInstanceState);
    }


    private View.OnClickListener mOnClickListener = new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            ImageMessageDialogFragment.this.dismiss();
        }
    };
}
